﻿using Microsoft.AspNetCore.Mvc;
using RWProxyDemo.Domains;
using RWProxyDemo.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using ZH.RetailContainer.CommLibrary;

namespace RWProxyDemo.Utils
{
    /// <summary>
    /// 封装结果
    /// </summary>
    public class PackResult
    {
        /// <summary>
        /// 封装正确结果，若只有状态不用传value，若返回查询结果需要传对应的查询结果集
        /// </summary>
        /// <param name="value">结果</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public static Result<T> Pack<T>(T value, State state = State.Succeed)
        {
            Result<T> result = new Result<T>();
            result.State = state;
            if (value != null)
                result.Data = value;
            return result;
        }

        /// <summary>
        /// 封装error信息
        /// </summary>
        /// <returns></returns>
        public static Result<object> Pack()
        {
            Result<object> result = new Result<object>();
            result.State = State.Succeed;
            return result;
        }

        /// <summary>
        /// 封装error信息
        /// </summary>
        /// <param name="code"></param>
        /// <param name="describe"></param>
        /// <returns></returns>
        public static Result<Error> Pack(ErrorCode code, string describe = "")
        {
            Error error = PackagError(code, describe);
            return Pack<Error>(error, State.Fail);
        }

        /// <summary>
        /// 封装error
        /// </summary>
        /// <param name="code"></param>
        /// <param name="describe"></param>
        /// <returns></returns>
        private static Error PackagError(ErrorCode code, string describe)
        {
            Error error = new Error();
            error.ErrorCode = ((int)code).ToString();
            if (string.IsNullOrWhiteSpace(describe))
                error.ErrorMessage = EnumHelper.GetDescription(code);
            else
                error.ErrorMessage = describe;
            return error;
        }
    }
}
